package com.jic.manager.controller.rest.sys;

import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.manager.api.rest.SysUserApi;
import com.jic.manager.entity.SysUser;
import com.jic.manager.response.SysUserResponse;
import com.jic.manager.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * description: ApiSysUserController
 * date: 2020/2/24 13:45
 * author: wangys
 * version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("api/sysUser")
public class ApiSysUserController implements SysUserApi {
    @Resource
    private RedisClientTemplate redisClientTemplate;
    @Resource
    private SysUserService sysUserService;


    @Override
    @PostMapping("getSysUserByToken")
    public RestResult<SysUserResponse> getSysUserByToken(@RequestParam("token") String token) {
        try {
            if (StringUtils.isEmpty(token)) throw new Exception("必传参数为空 token");
            String userStr = redisClientTemplate.get(token);
            if (StringUtils.isEmpty(userStr)){
                return RestResult.error("4000","登录失效");
            }
            SysUser sysUser = JSONArray.parseObject(userStr, SysUser.class);
            SysUser sysUserVo = sysUserService.queryByPrimaryKey(sysUser.getId());
            if (sysUserVo == null) throw new Exception("当前用户不存在 userId = "+sysUser.getId());
            if(sysUserVo.getDeleteFlag().equals(1)) throw new Exception("当前用户已被删除 userId = "+sysUserVo.getId());
            if (sysUserVo.getStatus() == 0){
                throw new Exception("当前用户已冻结 userId="+sysUserVo.getId());
            }else if(sysUserVo.getStatus() == 2){
                throw new Exception("当前用户未审核 userId="+sysUserVo.getId());
            }
            SysUserResponse sysUserResponse = new SysUserResponse();
            BeanUtils.copyProperties(sysUserVo,sysUserResponse);
            return RestResult.success(sysUserResponse);
        }catch (Exception e){
            e.printStackTrace();
            return RestResult.error("9999",e.getLocalizedMessage());
        }
    }

    @Override
    public RestResult<SysUserResponse> getSysUserById(Long id) {
       try {
           if (id == null || id == 0){
               throw new RuntimeException("必传参数不能为空");
           }
           SysUser sysUser = sysUserService.queryByPrimaryKey(id);
           SysUserResponse sysUserResponse = new SysUserResponse();
           BeanUtils.copyProperties(sysUser,sysUserResponse);
           return RestResult.success(sysUserResponse);
       }catch (Exception e){
           log.error(e.getLocalizedMessage());
           return RestResult.error("9999",e.getLocalizedMessage());
       }
    }

    @Override
    public List<String> userPhone() {
        List<String> phones = sysUserService.userPhone(new Date());
        return phones;
    }
}
